﻿#include "MainWork.h"
#include "newSettingWindow.h"
#include "xlsxdocument.h"

MainWork::MainWork(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    init();

}

MainWork::~MainWork()
{

}

void MainWork::init()
{
    listWidget = new CListWidget(this);
    tableWidget = new CTableWidget(this);
    tableDataModel = new CTableModel(this);
    listWidget->setModel(tableDataModel);
    listWidget->setContextMenuPolicy(Qt::NoContextMenu);//右键不显示菜单
    tableWidget->setContextMenuPolicy(Qt::NoContextMenu);

    ui.stackedWidget->addWidget(tableWidget);//加入堆栈窗口中
    ui.stackedWidget->addWidget(listWidget);



    connect(ui.actionNew, SIGNAL(triggered()), this, SLOT(onNewWorkClicked()));
    connect(ui.actionNewFile, SIGNAL(triggered()), this, SLOT(onNewWorkClicked()));
    connect(ui.actionAddWork,SIGNAL(triggered()),this,SLOT(onAddWorkClicked()));    //导入

    std::shared_ptr<database::DatabaseTask>task = std::make_shared<database::DatabaseTask>();
    task->id = CallbackDispatcher::getTaskId();
    task->type = database::OperationType::SELECT;
    task->sql = "SELECT * FROM tab_project";

    //结果回调-->信息一起同步封装在数据库任务中
    auto func = [this](QVariant p1, QVariant p2)->int {//参数列表：字段，查询结果列表集
        if (!p1.canConvert<QVector<QVariant>>() || !p2.canConvert<QVector<QVariantList>>())    return -1;

        QVector<QVariant> fields = p1.value<QVector<QVariant>>();
        if (fields.size() > MUST_VALID_COUNT)  return -2;

        QVector<QVariantList> result = p2.value<QVector<QVariantList>>();
        QVector<QVariant> headers = convertToHeaders(fields);   //获取表头

        tableDataModel->initModel(headers, result);
        tableWidget->setAutoResizeTable(true, false);

        return 0;
    };

    std::shared_ptr<CallbackContext> context = std::make_shared<CallbackContext>();
    context->callbackObject = this;
    context->successCallback = func;
    context->failCallback = nullptr;

    //任务数据封装完毕，开始注册任务
    CALLBACK_DISPATCHER->registerTask(task->id, context);
    DATABASE_THREAD->addTask(task);




    switch (viewtype)
    {
    case ViewType::TableView:
        viewtype = ViewType::ListView;      //为了通过槽函数内判断   
        on_actionMid_triggered();
        break;
    case ViewType::ListView:
        viewtype = ViewType::TableView;
        on_actionList_triggered();
        break;
    case MainWork::ViewType::SmallImageView:
        viewtype = ViewType::BigImageView;
        on_actionSmall_triggered();
        break;
    case MainWork::ViewType::BigImageView:
        viewtype = ViewType::SmallImageView;
        on_actionBig_triggered();
        break;
    default:
        break;
    }

}

/*
* @brief: 将字段名转换为表头
*/
QVector<QVariant> MainWork::convertToHeaders(QVector<QVariant> fields)
{
    int count = fields.size();
    QVector<QVariant> headers;
    headers.resize(count);
    for (int i = 0; i < count; ++i)
    {
        if (fields[i].toString() == "id") headers[i] = "工程ID";
        else if (fields[i].toString() == "name") headers[i] = "工程名称";
        else if (fields[i].toString() == "path") headers[i] = "路径";
        else if (fields[i].toString() == "resolution") headers[i] = "分辨率";
        else if (fields[i].toString() == "create_time") headers[i] = "创建时间";
        else if (fields[i].toString() == "description") headers[i] = "描述";
        else headers[i] = fields[i];
    }

    return headers;
}

/*
* @brief: 列表显示
*/
void MainWork::on_actionList_triggered()
{
    std::cout << __FUNCTION__ << std::endl;

    if (viewtype == ViewType::ListView) return;
    viewtype = ViewType::ListView;
    listWidget->setDisplayMode(CListWidget::ListMode);
    ui.stackedWidget->setCurrentWidget(listWidget);
}
/*
* @brief: 大图显示
*/
void MainWork::on_actionBig_triggered()
{
    std::cout << __FUNCTION__ << std::endl;

    if (viewtype = ViewType::BigImageView) return;
    viewtype = ViewType::BigImageView;
    listWidget->setDisplayMode(CListWidget::BigMode);
    ui.stackedWidget->setCurrentWidget(listWidget);

}
/*
* @brief: 详情显示
*/
void MainWork::on_actionMid_triggered()
{
    std::cout << __FUNCTION__ << std::endl;

    if (viewtype == ViewType::TableView) return;
    viewtype = ViewType::TableView;
    ui.stackedWidget->setCurrentWidget(tableWidget);
}

/*
* @brief: 小图显示
*/
void MainWork::on_actionSmall_triggered()
{
    std::cout << __FUNCTION__ << std::endl;

    if (viewtype == ViewType::SmallImageView) return;
    viewtype = ViewType::SmallImageView;
    listWidget->setDisplayMode(CListWidget::SmallMode);
    ui.stackedWidget->setCurrentWidget(listWidget);
}

void MainWork::onAddWorkClicked()
{
    QXlsx::Document xlsx("test.xlsx");

    xlsx.read("A1").toString();
    xlsx.read("B1").toString();
    xlsx.read("C1").toString();


}

void MainWork::onNewWorkClicked()
{
    newSettingWindow* newfileWindow = new newSettingWindow(this);
    newfileWindow->show();

}
